package com.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.adapter.base.ViewHolder;
import com.adapter.base.recyclerview.CommonAdapter;
import com.bean.GsonFormatTest;
import com.service.UpdateApkService;
import com.util.HttpUtil;
import com.util.R;
import com.util.TextUtil;
import com.util.ToastUtil;
import com.util.okhttputils.OkHttpUtils;
import com.util.okhttputils.callback.ObjectCallBack;
import com.util.okhttputils.callback.StringCallback;
import com.view.recyclerViewDivider.RecyclerViewLinearLayoutDivider;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by marlon on 2016-04-22.
 */
public class OkHttpActivity extends BaseActivity {
    @Bind(R.id.rv_okHttp)
    RecyclerView rv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_okhttp);
        ButterKnife.bind(this);
        initData();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //可以取消同一个tag的
        OkHttpUtils.getInstance().cancelTag(this);//取消以Activity.this作为tag的请求

    }

    @Override
    protected void initData() {
        super.initData();
        List<String> dataList = new ArrayList<>();
        dataList.add("异步Get请求String");
        dataList.add("异步Get请求Object对象");
        dataList.add("异步Post请求String");
        dataList.add("异步Post请求Object对象");
        dataList.add("异步上传文件");
        dataList.add("异步下载文件");
        rv.setLayoutManager(new LinearLayoutManager(OkHttpActivity.this, LinearLayoutManager.VERTICAL, false));//这里用线性显示 类似于listview
        rv.setAdapter(new CommonAdapter<String>(OkHttpActivity.this,R.layout.recyclerview_item_text,dataList) {

            @Override
            public void convert(final ViewHolder holder, String s) {
                holder.setText(R.id.tv_item_recyclerView, s);
                holder.setOnClickListener(R.id.tv_item_recyclerView, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (holder.getAdapterPosition()) {
                            case 0: {
                                Map<String, String> params = new HashMap<>();
                                params.put("Action", "GetRealTimeDayRainFall");
                                params.put("AdminArea", "广东");
                                params.put("Volley", "null");
                                params.put("DateTime", "2016-05-06");
                                HttpUtil.getResponse("http://183.232.33.177:8082/smb3/rainwater2/stpptnrnew", params, OkHttpActivity.this, new StringCallback() {
                                    @Override
                                    public void onError(Call call, Exception e) {

                                    }

                                    @Override
                                    public void onResponse(String response) {
                                        ToastUtil.showBottomToast(response);
                                    }
                                });
                            }
                            break;
                            case 1: {
                                Map<String, String> params = new HashMap<>();
                                params.put("Action", "GetRealTimeDayRainFall");
                                params.put("AdminArea", "广东");
                                params.put("Volley", "null");
                                params.put("DateTime", "2016-05-06");
                                HttpUtil.getResponse("http://183.232.33.177:8082/smb3/rainwater2/stpptnrnew", params, OkHttpActivity.this, new ObjectCallBack<GsonFormatTest>(GsonFormatTest.class) {
                                    @Override
                                    public void onSuccess(GsonFormatTest response) {
                                        if (response != null) {
                                            List<GsonFormatTest.InfoListBean> infoList = response.getInfoList();
                                            if (TextUtil.isValidate(infoList))
                                                ToastUtil.showBottomToast(infoList.get(0).toString());
                                        } else {

                                        }
                                    }

                                    @Override
                                    public void onFail(Call call, Exception e) {
                                        e.printStackTrace();
                                        ToastUtil.showBottomToast("网络异常");
                                    }
                                });
                            }
                            break;
                            case 2:
                                break;
                            case 3:
                                break;
                            case 4:
                                break;
                            case 5: {
                                Intent downloadIntent = new Intent(OkHttpActivity.this, UpdateApkService.class);
                                startService(downloadIntent);
                            }
                            break;
                        }

                    }
                });

            }
        });
        rv.addItemDecoration(new RecyclerViewLinearLayoutDivider(RecyclerViewLinearLayoutDivider.VERTICAL_LIST, getResources().getDrawable(R.drawable.sp_divider_listview)));
    }




}
